
// 日期时间处理工具类

// 格式化日期时间
const formatDateTime = {
  // 完整格式：2024-01-20 14:30
  full(date) {
    if (!date) return ''
    const d = new Date(date)
    const year = d.getFullYear()
    const month = (d.getMonth() + 1).toString().padStart(2, '0')
    const day = d.getDate().toString().padStart(2, '0')
    const hour = d.getHours().toString().padStart(2, '0')
    const minute = d.getMinutes().toString().padStart(2, '0')
    return `${year}-${month}-${day} ${hour}:${minute}`
  },

  // 仅日期：2024-01-20
  date(date) {
    if (!date) return ''
    const d = new Date(date)
    const year = d.getFullYear()
    const month = (d.getMonth() + 1).toString().padStart(2, '0')
    const day = d.getDate().toString().padStart(2, '0')
    return `${year}-${month}-${day}`
  },

  // 仅时间：14:30
  time(date) {
    if (!date) return ''
    const d = new Date(date)
    const hour = d.getHours().toString().padStart(2, '0')
    const minute = d.getMinutes().toString().padStart(2, '0')
    return `${hour}:${minute}`
  },

  // 友好显示：今天/明天/后天 14:30
  friendly(date) {
    if (!date) return ''
    const d = new Date(date)
    const now = new Date()
    const today = new Date(now.getFullYear(), now.getMonth(), now.getDate())
    const tomorrow = new Date(today)
    tomorrow.setDate(tomorrow.getDate() + 1)
    const afterTomorrow = new Date(today)
    afterTomorrow.setDate(afterTomorrow.getDate() + 2)

    const dateStr = (() => {
      const targetDate = new Date(d.getFullYear(), d.getMonth(), d.getDate())
      if (targetDate.getTime() === today.getTime()) return '今天'
      if (targetDate.getTime() === tomorrow.getTime()) return '明天'
      if (targetDate.getTime() === afterTomorrow.getTime()) return '后天'
      return `${d.getMonth() + 1}月${d.getDate()}日`
    })()

    return `${dateStr} ${formatDateTime.time(d)}`
  }
}

// 日期时间比较
const compareDateTime = {
  // 是否早于当前时间
  isBeforeNow(date) {
    if (!date) return false
    return new Date(date) < new Date()
  },

  // 是否晚于当前时间
  isAfterNow(date) {
    if (!date) return false
    return new Date(date) > new Date()
  },

  // 是否是今天
  isToday(date) {
    if (!date) return false
    const d = new Date(date)
    const today = new Date()
    return d.getDate() === today.getDate() &&
      d.getMonth() === today.getMonth() &&
      d.getFullYear() === today.getFullYear()
  },

  // 是否是明天
  isTomorrow(date) {
    if (!date) return false
    const d = new Date(date)
    const tomorrow = new Date()
    tomorrow.setDate(tomorrow.getDate() + 1)
    return d.getDate() === tomorrow.getDate() &&
      d.getMonth() === tomorrow.getMonth() &&
      d.getFullYear() === tomorrow.getFullYear()
  }
}

// 获取日期范围
const getDateRange = {
  // 获取一周的起止日期
  week(date = new Date()) {
    const current = new Date(date)
    const day = current.getDay()
    const diff = current.getDate() - day + (day === 0 ? -6 : 1)
    const monday = new Date(current.setDate(diff))
    const sunday = new Date(current)
    sunday.setDate(sunday.getDate() + 6)
    return {
      start: monday,
      end: sunday
    }
  },

  // 获取一月的起止日期
  month(date = new Date()) {
    const current = new Date(date)
    const firstDay = new Date(current.getFullYear(), current.getMonth(), 1)
    const lastDay = new Date(current.getFullYear(), current.getMonth() + 1, 0)
    return {
      start: firstDay,
      end: lastDay
    }
  }
}

// 验证日期时间
const validateDateTime = {
  // 验证是否是有效的日期时间
  isValid(date) {
    if (!date) return false
    const d = new Date(date)
    return d instanceof Date && !isNaN(d)
  },

  // 验证是否是未来时间
  isFuture(date) {
    if (!this.isValid(date)) return false
    return new Date(date) > new Date()
  },

  // 验证是否在指定范围内
  isInRange(date, startDate, endDate) {
    if (!this.isValid(date)) return false
    const d = new Date(date)
    const start = startDate ? new Date(startDate) : null
    const end = endDate ? new Date(endDate) : null
    
    if (start && end) {
      return d >= start && d <= end
    } else if (start) {
      return d >= start
    } else if (end) {
      return d <= end
    }
    return true
  }
}

module.exports = {
  formatDateTime,
  compareDateTime,
  getDateRange,
  validateDateTime
}